package com.foryang.springcloud.alibaba.service.impl;

import com.foryang.springcloud.alibaba.dao.AccountDao;
import com.foryang.springcloud.alibaba.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class AccountServiceImpl implements AccountService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Resource
    private AccountDao storageDao;


    @Override
    public void decrease(Long userId, BigDecimal money) {
        LOGGER.info("--------->storage-service中扣减库存开始");
        //模拟超时异常，全局事务回滚
        try {
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        storageDao.decrease(userId,money);
        LOGGER.info("--------->storage-service中扣减库存结束");
    }
}
